package com.gewu.pm.ui.activity.promotion;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.aop.SingleClick;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.bean.GoodsBean;
import com.gewu.pm.other.IntentKey;
import com.gewu.pm.ui.adapter.order.GoodsAdapter;
import com.gewu.pm.utils.eventbus.EventBean;
import com.gewu.pm.utils.eventbus.EventBusUtil;
import com.gewu.pm.utils.eventbus.EventCode;

import java.util.ArrayList;
import java.util.List;

/**
 * 促销主品/赠品页面
 * 
 * type=1: 促销主品
 * type=2: 促销赠品
 */
public final class PromotionProductActivity extends AppActivity {

    private RecyclerView mRecyclerView;
    private TextView mVarietyCountView;
    private TextView mTotalCountView;
    private TextView mConfirmButton;

    private GoodsAdapter mAdapter;
    private List<GoodsBean> mGoodsList = new ArrayList<>();

    private int mType;  // 1-促销主品，2-促销赠品

    public static void start(Context context, int type, List<GoodsBean> goodsList) {
        Intent intent = new Intent(context, PromotionProductActivity.class);
        intent.putExtra(IntentKey.TYPE, type);
        intent.putExtra(IntentKey.DATA, (java.io.Serializable) goodsList);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.promotion_product_activity;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mRecyclerView = findViewById(R.id.rv_goods_list);
        mVarietyCountView = findViewById(R.id.tv_variety_count);
        mTotalCountView = findViewById(R.id.tv_total_count);
        mConfirmButton = findViewById(R.id.tv_bottom_button);

        // 设置 RecyclerView
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new GoodsAdapter(this);
        mAdapter.setShowDelete(true);  // 显示删除按钮
        mAdapter.setOnQuantityChangeListener(new GoodsAdapter.OnQuantityChangeListener() {
            @Override
            public void onQuantityChanged() {//数量变化
                updateStatistics();
            }

            @Override
            public void onStockInsufficient(String goodsName) {//库存不足

            }

            @Override
            public void onDeleteGoods(int position) {//删除商品

            }
        });
        mRecyclerView.setAdapter(mAdapter);

        setOnClickListener(mConfirmButton);
    }

    @Override
    protected void initData() {
        mType = getInt(IntentKey.TYPE, 1);

        // 设置标题
        setTitle(mType == 1 ? "促销主品" : "促销赠品");

        // 接收商品数据
        java.io.Serializable data = getSerializable(IntentKey.DATA);
        if (data instanceof List) {
            mGoodsList = (List<GoodsBean>) data;
        }

        mAdapter.setData(mGoodsList);
        updateStatistics();
    }

    /**
     * 更新统计信息
     */
    private void updateStatistics() {
        int varietyCount = 0;
        int totalCount = 0;
        
        // 只统计数量大于0的商品
        for (GoodsBean goods : mGoodsList) {
            if (goods.getQuantity() > 0) {
                varietyCount++;
                totalCount += goods.getQuantity();
            }
        }
        
        mVarietyCountView.setText("品种数量：" + varietyCount);
        mTotalCountView.setText("商品总数：" + totalCount);
        
        // 如果全部删除（数量都为0），关闭页面并发送事件
        if (varietyCount == 0) {
            sendGoodsChangedEvent();
            finish();
        }
    }

    @SingleClick
    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view == mConfirmButton) {
            // 确定
            sendGoodsChangedEvent();
            finish();
        }
    }

    /**
     * 发送商品变更事件
     */
    private void sendGoodsChangedEvent() {
        // 传递类型和商品列表
        Object[] eventData = new Object[]{mType, mGoodsList};
        EventBusUtil.sendEvent(new EventBean<>(EventCode.PROMOTION_GOODS_CHANGED, eventData));
    }
}

